//
//  MyScoketTestViewController.m
//  MyScoketTest
//
//  Created by wilson_chen on 2009/12/30.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
//
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <CFNetwork/CFSocketStream.h>
#include <arpa/inet.h>

#import "MyScoketTestViewController.h"
#import "proto.h"

@implementation MyScoketTestViewController

@synthesize label;
@synthesize textField;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (IBAction) doStart:(id)sender
{
	int ret;
	char *buf;

	//[sender resignFirstResponder];
	label.text = textField.text;
	textField.text = @"";
	//udp = [MyUDP new];
	//NSError *error;
	//if (udp == nil || ![udp start:&error])
	//	NSLog(@"Failed creating server: %@", error);
	//textField.text = [NSString stringWithFormat:@"%@", udp.port];
	//int ret;
	//ret = [udp senddata];
	//textField.text = [NSString stringWithFormat:@"%@", ret];

    struct sockaddr_in addr4;
    memset(&addr4, 0, sizeof(addr4));
    addr4.sin_len = sizeof(addr4);
    addr4.sin_family = AF_INET;
    addr4.sin_port = htons(30003);
    //addr4.sin_addr.s_addr = htonl(INADDR_ANY);
    addr4.sin_addr.s_addr = inet_addr((char*)@"122.147.6.109");
    NSData *address4 = [NSData dataWithBytes:&addr4 length:sizeof(addr4)];

	buf = (char*)malloc(sizeof(PKTHeader) + sizeof(LoginMessage_t));
	
	memset(buf, 0, sizeof(PKTHeader) + sizeof(LoginMessage_t));
	
	PKTHeader *header = (PKTHeader *)buf;
	LoginMessage_t *user = (LoginMessage_t *)(buf + sizeof(PKTHeader));
	
	header->verId = P2P_VERID;
	header->nid = MSGTYPE_HPREQ;
	header->data_length = htons(sizeof(LoginMessage_t));
	
	sprintf(user->username, (char*)@"%@", @"AAAA0001");
	sprintf(user->password, (char*)@"%@", @"12345678");
	
	NSData *data = [NSData dataWithBytes:buf length:sizeof(PKTHeader)+sizeof(LoginMessage_t)];
	
	ret = CFSocketSendData(_ipv4socket, (CFDataRef)address4, (CFDataRef)data, (CFTimeInterval)1000);
}

@end
